package com.yonyougov.devops.cruxcisupport.envconfigitem;

import com.yonyougov.devops.cruxcisupport.env.EnvEntity;
import com.yonyougov.devops.cruxcisupport.env.EnvService;
import com.yonyougov.devops.cruxcisupport.env.dto.config.*;
import com.yonyougov.devops.cruxcisupport.k8s.rest.service.K8sDeploymentService;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.service.BaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;

/**
 * Created by ligson on 2017/8/11.
 */
@RestController
@RequestMapping("/api/v2/envconfigitem")
public class EnvConfigItemController extends CrudController<EnvConfigItemEntity, BigInteger> {

    @Autowired
    private K8sDeploymentService k8sDeploymentService;

    @Autowired
    private EnvService envService;

    private final EnvConfigItemService envConfigItemService;

    public EnvConfigItemController(EnvConfigItemService envConfigItemService) {
        this.envConfigItemService = envConfigItemService;
    }

    @Override
    public BaseService<EnvConfigItemEntity, BigInteger> getService() {
        return envConfigItemService;
    }

    //<!--用户自定义代码开始-->
    @PostMapping(value = "/eci/add")
    public BaseResponseDto addEnvConfigItem(@RequestBody @Valid AddOneEnvConfigItemRequestDto requestDto) throws Exception {
        EnvConfigItemEntity envConfigItemEntity = new EnvConfigItemEntity();
        BeanUtils.copyProperties(requestDto, envConfigItemEntity);

        envConfigItemService.add(envConfigItemEntity);

        // 更新k8s configMap
        EnvEntity envEntity = envService.findBy(EnvEntity.builder().id(requestDto.getEnvId()).build());
        k8sDeploymentService.createConfigMap(requestDto.getEnvId(),envEntity.getName());

        return BaseResponseDto.getSuccessInstance();
    }
    //<!--用户自定义代码结束-->

    @PostMapping(value = "/eci/update")
    public BaseResponseDto updateEnvConfigItem(@RequestBody @Valid UpdateOneEnvConfigItemRequestDto requestDto) throws Exception {
        EnvConfigItemEntity envConfigItemEntity = new EnvConfigItemEntity();
        BeanUtils.copyProperties(requestDto, envConfigItemEntity);

        envConfigItemService.update(envConfigItemEntity);

        // 更新k8s configMap
        EnvEntity envEntity = envService.findBy(EnvEntity.builder().id(requestDto.getEnvId()).build());
        k8sDeploymentService.createConfigMap(requestDto.getEnvId(),envEntity.getName());

        return BaseResponseDto.getSuccessInstance();
    }

    @PostMapping(value = "/eci/page")
    public EnvConfigItemQueryResponseDto findEnvConfigItemPage(@RequestBody QueryEnvConfigItemRequestDto requestDto) {
        EnvConfigItemEntity envConfigItemEntity = new EnvConfigItemEntity();
        BeanUtils.copyProperties(requestDto, envConfigItemEntity);

        return envConfigItemService.findPageBy(envConfigItemEntity);
    }

    @PostMapping(value = "/eci/delete/id")
    public BaseResponseDto deleteEnvConfigItemById(@RequestBody @Valid DeleteOneEnvConfigItemRequestDto requestDto) {
        envConfigItemService.deleteByPrimaryKey(requestDto.getId());

        return BaseResponseDto.getSuccessInstance();
    }
}
